package GUI;

import ModelLayer.*;
import CtrlLayer.*;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUI extends JFrame implements ActionListener{
    private Container container;
    private JLabel errorMessage;
    private JMenuItem exitItem;
    private JPanel panelLogIn;
    private JPanel panelMenu;
    private int sessionId;
    private JTextField textId;
    private JPasswordField textPassword;
    ProductsGUI product;
    CustomersGUI customer;
    ContractorsGUI contractor;

    public GUI(){
        panelLogIn  = new JPanel(new GridBagLayout());
        panelMenu = new JPanel(new GridBagLayout());
        textId = new JTextField();
        textPassword = new JPasswordField();
        SessionController session = new SessionController();
        this.sessionId = session.createSession();
        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(400, 470);
        this.setResizable(false);
        mainMenuStart();
        //         makeMenuBar();

    }

    public void mainMenuStart(){
        textId.setText("");
        textPassword.setText("");
        if(SessionContainer.getInstance().findSessionById(sessionId).isLoggedIn()){
            makeMenuPanel();
            this.getContentPane().add(panelMenu, BorderLayout.NORTH);
        }else{
            makeLogInPanel();
            this.getContentPane().add(panelLogIn, BorderLayout.NORTH);
        }
    }

    public void makeMenuBar(){
        JMenuItem productsItem;  
        JMenuItem customersItem;  
        JMenuItem contractorsItem;
        JMenuItem salesItem; 
        JMenuItem leasesItem; 
        JMenuItem ordersItem; 
        JMenuItem deliveriesItem;
        JMenuItem notesItem;
        JMenuItem employeesItem;
        JMenuItem statisticsItem;
        JMenuItem managersItem;
        JMenuItem backItem;
        JMenuItem logOutItem;

        JMenuBar menuBar = new JMenuBar();

        JMenu menuMenu = new JMenu("Menu");

        productsItem = new JMenuItem("Products");
        productsItem.addActionListener(this);
        menuMenu.add(productsItem);

        customersItem = new JMenuItem("Customers");
        customersItem.addActionListener(this);
        menuMenu.add(customersItem);

        contractorsItem = new JMenuItem("Contractors");
        contractorsItem.addActionListener(this);
        menuMenu.add(contractorsItem);

        salesItem = new JMenuItem("Sales");
        salesItem.addActionListener(this);
        menuMenu.add(salesItem);

        leasesItem = new JMenuItem("Leases");
        leasesItem.addActionListener(this);
        menuMenu.add(leasesItem);

        ordersItem = new JMenuItem("Orders");
        ordersItem.addActionListener(this);
        menuMenu.add(ordersItem);

        deliveriesItem = new JMenuItem("Deliveries");
        deliveriesItem.addActionListener(this);
        menuMenu.add(deliveriesItem);

        notesItem = new JMenuItem("Notes");
        notesItem.addActionListener(this);
        menuMenu.add(notesItem);

        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            employeesItem = new JMenuItem("Employees");
            employeesItem.addActionListener(this);
            menuMenu.add(employeesItem);

            statisticsItem = new JMenuItem("Statistics");
            statisticsItem.addActionListener(this);
            menuMenu.add(statisticsItem);

            managersItem = new JMenuItem("Managers");
            managersItem.addActionListener(this);
            menuMenu.add(managersItem);
        }

        logOutItem = new JMenuItem("Log out");
        logOutItem.addActionListener(this);
        logOutItem.setEnabled(false); 
        menuMenu.add(logOutItem);

        exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(this);
        menuMenu.add(exitItem);

        menuBar.add(menuMenu);

        setJMenuBar(menuBar);
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource() == exitItem){
            System.exit(0);
        }
    }

    public void makeLogInPanel(){
        JLabel labelHeader;
        JLabel labelId;
        JLabel labelPassword;
        JButton logIn;
        JButton clear;
        GridBagConstraints gbc = new GridBagConstraints();

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelLogIn.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100, 25));
        panelLogIn.add(textId, gbc);

        labelPassword = new JLabel("Password:", JLabel.RIGHT);
        labelPassword.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelLogIn.add(labelPassword, gbc);

        textPassword.setEchoChar('*');
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textPassword.setPreferredSize(new Dimension(100, 25));
        panelLogIn.add(textPassword, gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelLogIn.add(errorMessage, gbc);

        logIn = new JButton("Log In");
        logIn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    if(logIn()){
                        container = getContentPane();
                        container.removeAll();
                        mainMenuStart();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                    else{
                        errorMessage.setText("Not possible!");
                        logIn.setEnabled(false);
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        logIn.setPreferredSize(new Dimension(100, 25));
        panelLogIn.add(logIn, gbc);

        clear = new JButton("Clear");
        clear.addActionListener(new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textPassword.setText("");
                    textId.setText("");
                    errorMessage.setText("");
                    logIn.setEnabled(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        clear.setPreferredSize(new Dimension(100, 25));
        panelLogIn.add(clear, gbc);
    }

    public boolean logIn(){
        boolean login = false;
        int id;
        String password = textPassword.getText();
        try{
            id = Integer.parseInt(textId.getText()) ;
            login = SessionContainer.getInstance().findSessionById(sessionId).login(id, password);
        }catch(Exception ex){

        }

        if(login)
            return true;
        else
            return false;
    }

    public void logOut(){
        //         System.out.println(SessionContainer.getInstance().findSessionById(sessionId).isLoggedIn());
        SessionContainer.getInstance().findSessionById(sessionId).logOut();
        
        
        this.textId.setText("");
        this.textPassword.setText("");
    }

    public void makeMenuPanel(){

        JButton products;
        JButton customers;
        JButton contractors;
        JButton sales;
        JButton leases;
        JButton orders;
        JButton deliveries;
        JButton notes;
        JButton employees;
        JButton statistics;
        JButton managers;
        JButton logOut;

        GridBagConstraints gbc = new GridBagConstraints();

        products = new JButton("Products");
        products.setPreferredSize(new Dimension(110, 25));
        products.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    ProductsGUI productsGUI = new ProductsGUI(sessionId);
                    productsGUI.makeProductsMenu();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(products, gbc);

        customers = new JButton("Customers");
        customers.setPreferredSize(new Dimension(110, 25));
        customers.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    CustomersGUI customersGUI = new CustomersGUI(sessionId);
                    customersGUI.customersMainMenu();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(customers, gbc);

        contractors = new JButton("Contractors");
        contractors.setPreferredSize(new Dimension(110, 25));
        contractors.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    ContractorsGUI contractorsGUI = new ContractorsGUI(sessionId);
                    contractorsGUI.contractorsMainMenu();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(contractors, gbc);

        sales = new JButton("Sales");
        sales.setPreferredSize(new Dimension(110, 25));
        sales.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    SalesGUI salesGUI = new SalesGUI(sessionId);
                    salesGUI.makeMenuSales();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(sales, gbc);

        leases = new JButton("Leases");
        leases.setPreferredSize(new Dimension(110, 25));
        leases.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    LeasesGUI leasesGUI = new LeasesGUI(sessionId);
                    leasesGUI.makeMenuLeases();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(leases, gbc);

        orders = new JButton("Orders");
        orders.setPreferredSize(new Dimension(110, 25));
        orders.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    OrdersGUI ordersGUI = new OrdersGUI(sessionId);
                    ordersGUI.makeMenuOrders();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(orders, gbc);

        deliveries = new JButton("Deliveries");
        deliveries.setPreferredSize(new Dimension(110, 25));
        deliveries.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    DeliveriesGUI deliveriesGUI = new DeliveriesGUI(sessionId);
                    deliveriesGUI.makeDeliveriesMenu();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(deliveries, gbc);

        notes = new JButton("Notes");
        notes.setPreferredSize(new Dimension(110, 25));
        notes.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    NotesGUI notesGUI = new NotesGUI();
                    notesGUI.makeNotesMenu();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 8;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(notes, gbc);

        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            employees = new JButton("Employes");
            employees.setPreferredSize(new Dimension(110, 25));
            employees.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        EmployeesGUI employeesGUI = new EmployeesGUI(sessionId);
                        employeesGUI.employeesMainMenu();
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 9;
            gbc.insets = new Insets(5,5,5,5);
            panelMenu.add(employees, gbc);

            statistics = new JButton("Statistics");
            statistics.setPreferredSize(new Dimension(110, 25));
            statistics.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        StatisticsGUI statisticsGUI = new StatisticsGUI();
                        statisticsGUI.makeStatisticsMenu();
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 10;
            gbc.insets = new Insets(5,5,5,5);
            panelMenu.add(statistics, gbc);

            managers = new JButton("Managers");
            managers.setPreferredSize(new Dimension(110, 25));
            managers.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        ManagersGUI managersGUI = new ManagersGUI();
                        managersGUI.managersMainMenu();
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 11;
            gbc.insets = new Insets(5,5,5,5);
            panelMenu.add(managers, gbc);
        }

        logOut = new JButton("Log out");
        logOut.setPreferredSize(new Dimension(110, 25));
        logOut.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    logOut();
                    panelMenu.removeAll();
                    container = getContentPane();
                    container.removeAll();
                    
                    mainMenuStart();
                    //                     
                    //                     makeLogInPanel();
                    //                     container.add(panelLogIn, BorderLayout.NORTH);
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 12;
        gbc.insets = new Insets(5,5,5,5);
        panelMenu.add(logOut, gbc);
    }

}
